package com.school.fuyuan.business.controller;

import com.school.fuyuan.business.model.po.Role;
import com.school.fuyuan.business.service.RoleService;
import com.school.fuyuan.common.annotation.Authenticate;
import com.school.fuyuan.common.message.RequestMsg;
import com.school.fuyuan.common.message.ResponseMsg;
import com.school.fuyuan.common.util.ParameterUtils;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * @author zrhong
 * @version 1.0
 * @date 2019/3/26 21:02
 * @description
 */
@RestController
@RequestMapping("role")
public class RoleController {
    private final RoleService roleService;

    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    /**
     * 获取角色
     * @return
     */
    @PostMapping("/getRoles")
    @ResponseBody
    @Authenticate(openAuth = true,authItems = {"/info/role/getRoles"})
    public ResponseMsg<List<Role>> getRoles() {
        List<Role> roles = roleService.getRoles();
        ResponseMsg responseMsg = new ResponseMsg();
        responseMsg.setData(roles);
        return responseMsg;
    }

    /**
     * 新增角色
     * @return
     */
    @PostMapping("/addRole")
    @ResponseBody
    @Authenticate(openAuth = true,authItems = {"/info/role/addRole"})
    public ResponseMsg addRole(@RequestBody @Valid RequestMsg<Map<String,Object>> requestMsg) {
        Map<String,Object> map = requestMsg.getData();
        String roleName = (String) map.get("roleName");
        List<String> privileges = (List<String>) map.get("privileges");
        ParameterUtils.assertValue("角色名称", roleName);
        ParameterUtils.assertValue("权限", privileges);
        roleService.addRole(roleName, privileges);
        ResponseMsg responseMsg = new ResponseMsg();
        return responseMsg;
    }

    /**
     * 删除角色
     * @return
     */
    @PostMapping("/deleteRole")
    @ResponseBody
    @Authenticate(openAuth = true,authItems = {"/info/role/deleteRole"})
    public ResponseMsg deleteRole(@RequestBody @Valid RequestMsg<String> requestMsg) {
        String roleId = requestMsg.getData();
        ParameterUtils.assertValue("角色Id", roleId);
        roleService.deleteRole(roleId);
        ResponseMsg responseMsg = new ResponseMsg();
        return responseMsg;
    }
}
